package com.market.controller;

import com.market.Dto.ProductQueryDto;
import com.market.Dto.ProductSaveDto;
import com.market.Vo.ProductQueryVo;
import com.market.entity.Product;
import com.market.service.ProductService;
import com.market.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 商品分页查询
     * @param productQueryDto
     * @return
     */
    @GetMapping("/product/list")
    public Result<ProductQueryVo> list(ProductQueryDto productQueryDto){
        return Result.success(productService.list(productQueryDto));
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        return Result.success(productService.upload(file));
    }

    /**
     * 添加商品
     * @param productSaveDto
     * @return
     */
    @PostMapping("/product/add")
    public Result<String> add(@RequestBody ProductSaveDto productSaveDto){
        return Result.success(productService.add(productSaveDto));
    }

    /**
     * 获取商品详情
     * @param id
     * @return
     */
    @GetMapping("/product/{id}")
    public Result<Product> getById(@PathVariable Long id){
        return Result.success(productService.getById(id));
    }

    /**
     * 修改商品
     * @param productSaveDto
     * @return
     */
    @PutMapping("/product/update")
    public Result<String> update(@RequestBody ProductSaveDto productSaveDto){
        productService.update(productSaveDto);
        return Result.success();
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/product/delete/{id}")
    public Result<String> delete(@PathVariable Long id){
        productService.delete(id);
        return Result.success();
    }
}
